package com.jali.crud.controller;

import com.jali.crud.bean.*;
import com.jali.crud.bean.JaAward;
import com.jali.crud.bean.JaScoreLog;
import com.jali.crud.bean.Msg;
import com.jali.crud.service.JaAwardService;
import com.jali.crud.service.JaScoreLogService;
import com.jali.crud.service.JaUserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jali.common.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @author jali
 * @create 2019-01-27 15:07
 */
@Controller
public class JaAwardController {

    @Autowired
    JaAwardService jaAwardService;

    @Autowired
    JaUserService jaUserService;

    @Autowired
    JaScoreLogService jaScoreLogService;

    @RequestMapping("/jaAwardList")
    public String toJaScoreUseList(){
        return "jaAwardList";
    }

    @RequestMapping("/getAwardList")
    @ResponseBody
    public Msg getAwardList(@RequestParam(value = "pn",defaultValue = "1") Integer pn){
        //在查询之前只需要调用，传入页码，以及每页的大小
        PageHelper.startPage(pn,5);
        List<JaAward> jaAwardList = jaAwardService.selectJaAwardList(null);
        //使用pageInfo包装查询后的结果，只需要将pageInfo交给页面就行了
        //封装了详细的分页详细，包括有我们查询出来的数据，传入连续显示的页数
        PageInfo pageInfo = new PageInfo(jaAwardList,5);
        return Msg.success().add("pageInfo",pageInfo);
    }

    @RequestMapping(value = "/getAward/{id}",method = RequestMethod.GET)
    @ResponseBody
    public Msg getEmp(@PathVariable Integer id){
        JaAward jaAward = jaAwardService.getAward(id);
        int myScore = jaUserService.getScoreByUserId("1");
        return Msg.success().add("jaAward",jaAward).add("myScore",myScore).add("convertible",myScore/jaAward.getScore());
    }

    @RequestMapping(value = "/convAward/{id}",method = RequestMethod.PUT)
    @ResponseBody
    public Msg convAward(JaAward jaAward){
        //用户兑换的数量
        int amount = jaAward.getInventory();
        JaAward ja = jaAwardService.getAward(jaAward.getId());
        //检查积分够不够
        int needScore = ja.getScore()*jaAward.getInventory();
        if(jaUserService.getScoreByUserId("1")<needScore){
            return Msg.fail().add("va_msg","您的积分不足");
        }

        //检查库存够不够
        if(ja.getLeftInve()<amount){
            return Msg.fail().add("va_msg","库存不足");
        }

        //创建兑换历史记录
        JaScoreLog jaScoreLog = new JaScoreLog();
        jaScoreLog.setInfo("兑换《"+ja.getName()+"》");
        jaScoreLog.setAmount(jaAward.getInventory());
        jaScoreLog.setProductid(jaAward.getId());
        jaScoreLog.setUserid(1);
        jaScoreLog.setTime(DateUtil.dateToString(new Date()));
        jaScoreLog.setScore(ja.getScore()*amount);

        //扣减积分
        jaUserService.updateScoreDeductToUser(1,jaScoreLog.getScore());
        //扣减库存
        ja.setLeftInve(ja.getLeftInve()-amount);
        jaAwardService.updateByPrimaryKeySelective(ja);
        jaScoreLogService.insertSelective(jaScoreLog);

        return Msg.success().add("va_msg","恭喜您，兑换成功");
    }
}
